package com.zhaosc.invoke;

import java.util.List;

import com.alibaba.fastjson.JSONObject;
import com.zhaosc.configbean.Reference;
import com.zhaosc.loadbalance.LoadBalance;
import com.zhaosc.loadbalance.NodeInfo;
import com.zhaosc.rpc.http.HttpRequest;

public class HttpInvoke implements Invoke{

	@Override
	public String invoke(Invocation invocation) {
		List<String> registryInfo=invocation.getReference().getRegistryInfo();
		
		//负载均衡算法
		Reference reference=invocation.getReference();
		
		String loadBlance=reference.getLoadBalance();
		LoadBalance loadBalance= reference.getLoadBalances().get(loadBlance);
		NodeInfo nodeinfo=loadBalance.doSelect(registryInfo);
		
		JSONObject sendParam=new JSONObject();
		sendParam.put("methodName", invocation.getMethod().getName());
		sendParam.put("methodParams", invocation.getParams());
		sendParam.put("serviceId", reference.getId());
		sendParam.put("paramTypes", invocation.getMethod()
                   .getParameterTypes());
           
		String url = "http://" + nodeinfo.getHost() + ":"
                + nodeinfo.getPort() + nodeinfo.getContextpath();
        //调用对端的生产者的服务
        String result = HttpRequest.sendPost(url, sendParam.toJSONString());
        return result;
	
	}

}
